#include "service.h"
#include "z_logger.h"
#include "model.h"
#include "aliot.h"
#include "cJSON.h"
#include "core_mqtt.h"
#include "aiot_devinfo_api.h"
#include "devinfo_private.h"

void _devinfo_recv_handler(void *handle, const aiot_devinfo_recv_t *packet, void *userdata) {
    switch (packet->type) {
        case AIOT_DEVINFORECV_GENERIC_REPLY:
            log_trace("msg_id=%d, code=%d, product=%s, device=%s, message=%.*s, data=%.*s",
                      packet->data.generic_reply.msg_id,
                      packet->data.generic_reply.code,
                      packet->product_key,
                      packet->device_name,
                      packet->data.generic_reply.message_len,
                      packet->data.generic_reply.message,
                      packet->data.generic_reply.data_len,
                      packet->data.generic_reply.data);
            break;
        default:
            break;
    }
}

void _devinfo_event_handler_t(void *handle, const aiot_devinfo_event_t *event, void *userdata) {
    switch (event->type) {
        case AIOT_DEVINFOEVT_INVALID_DEVINFO:
            log_debug("invalid devinfo");
            break;
        case AIOT_DEVINFOEVT_INVALID_RESPONSE:
            log_debug("invalid response");
            break;
        case AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT:
            log_debug("invalid response format");
            break;
        default:
            break;
    }
}

void *_devinfo_service_init(aliot_service_args_t *args) {
    void *devinfo_handle = aiot_devinfo_init();

    aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_MQTT_HANDLE, args->mqtt_handle);
    aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_RECV_HANDLER, _devinfo_recv_handler);
    aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_EVENT_HANDLER, _devinfo_event_handler_t);
    aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_USERDATA, args->seqiot);

    return devinfo_handle;
}

static void _devinfo_service_fini(void **handle) {
    aiot_devinfo_deinit(handle);
}

static void *_devinfo_service_start(void *handle) {
    devinfo_handle_t *devinfo_handle = handle;
    seqiot_t *seqiot = devinfo_handle->userdata;

    aiot_devinfo_msg_t msg;
    bzero(&msg, sizeof(aiot_devinfo_msg_t));

    msg.type = AIOT_DEVINFO_MSG_UPDATE;
    msg.product_key = core_mqtt_get_product_key(devinfo_handle->mqtt_handle);
    msg.device_name = core_mqtt_get_device_name(devinfo_handle->mqtt_handle);

    cJSON *cjson = cJSON_CreateArray();

    cJSON *attr = cJSON_CreateObject();
    cJSON_AddItemToArray(cjson, attr);

    cJSON_AddStringToObject(attr, "attrKey", "iccid");
    cJSON_AddStringToObject(attr, "attrValue", "8986000000");

    msg.data.update.params = cJSON_PrintUnformatted(cjson);
    cJSON_Delete(cjson);

    int32_t res = aiot_devinfo_send(handle, &msg);
    aliot_error_check("aiot_devinfo_send", res);
    cJSON_free(msg.data.update.params);
}

static int32_t _devinfo_service_stop(void *args) {
    return STATE_SUCCESS;
}

static aliot_service_t _devinfo_service = {
        .name = DEVINFO_MODULE_NAME,
        .init = _devinfo_service_init,
        .fini = _devinfo_service_fini,
        .start = _devinfo_service_start,
        .stop = NULL
};

aliot_service_t *devinfo_service_get() {
    return &_devinfo_service;
}